package datos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import javax.swing.JOptionPane;

public class DatabaseManager {
    private Connection connection;
    private Savepoint svpt;
  
    public DatabaseManager() {
        try {
            String url = "jdbc:postgresql://localhost/TrabajoFinal?user=postgres&password=pac_08ev";
            System.out.println(url);
            connection = DriverManager.getConnection(url);
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace(); }
     }

    public void commit() {
        try {
            connection.commit();
            svpt = connection.setSavepoint();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() {
        return connection;
  
    }
    //en caso que necesite hacer conexiones paralelas

    public void openConnectionTo(String url) {
        try {
            connection = DriverManager.getConnection(url, "postgres", "clave");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
  
    public void rollBack() {
        try {
            if (svpt != null) {
                connection.rollback(svpt);
                connection.commit();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
